package pers.zw.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ServletSessionDemo1", urlPatterns = "/ServletSessionDemo1")
public class ServletSessionDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset = UTF-8");
        response.setCharacterEncoding("UTF-8");

        HttpSession session = request.getSession();

        String sessionId = session.getId();

        if (session.isNew()) {
            response.getWriter().print("Session创建成功，id: " + sessionId);
        } else {
            response.getWriter().print("服务器已存在该session, id: " + sessionId);
        }

    }
}
